package com.huike.web.controller.review;


import com.huike.business.domain.TbBusiness;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.CustomException;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * =========================================================新增数据============================================
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable String name,
                           @PathVariable Integer age,
                           @PathVariable String sex) {
        if (name == null) {
            throw new CustomException("name不能为空");
        }
        if (age == null) {
            throw new CustomException("age不能为空");
        }
        if (sex == null) {
            throw new CustomException("sex不能为空");
        }
        return toAjax(reviewService.save(name, age, sex));
    }

    @PostMapping("/saveData")
    public AjaxResult save2(@RequestBody MybatisReviewVO mybatisReviewVO) {
        if (mybatisReviewVO.getName() == null) {
            throw new CustomException("name不能为空");
        }
        if (mybatisReviewVO.getAge() == null) {
            throw new CustomException("Age不能为空");
        }
        if (mybatisReviewVO.getSex() == null) {
            throw new CustomException("Sex不能为空");
        }
        return toAjax(reviewService.save2(mybatisReviewVO));

    }

    /**
     * =========================================================删除数据=============================================
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        return toAjax(reviewService.deleteById(id));
    }

    /**
     * =========================================================修改数据=============================================
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MybatisReviewVO mybatisReviewVO) {
        return toAjax(reviewService.update(mybatisReviewVO));
    }

    /**
     * =========================================================查询数据=============================================
     */
    @GetMapping("/getById")
    public AjaxResult getById(MybatisReviewVO mybatisReviewVO) {
        return AjaxResult.success(reviewService.getById(mybatisReviewVO.getId()));
    }

    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage() {
        startPage();
        List<Review> list =reviewService.selectReview();
        return getDataTable(list);
    }
}